package com.dycx.cms.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dycx.cms.mapper.ArticleRecommendTypeMapper;
import com.dycx.cms.dto.ArticleRecommendTypeDTO;
import com.dycx.cms.entity.ArticleRecommendDO;
import com.dycx.cms.entity.ArticleRecommendTypeDO;
import com.dycx.cms.service.ArticleRecommendService;
import com.dycx.cms.service.ArticleRecommendTypeService;
import com.dycx.cms.vo.ArticleRecommendTypeVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 推荐类型(ArticleRecommendType)表服务实现类
 *
 * @author wuzongbin
 * @since 2023-05-25 10:48:13
 */

@Service
@AllArgsConstructor
public class ArticleRecommendTypeServiceImpl extends ServiceImpl<ArticleRecommendTypeMapper, ArticleRecommendTypeDO> implements ArticleRecommendTypeService {

    private ArticleRecommendTypeMapper articleRecommendTypeMapper;
    private ArticleRecommendService articleRecommendService;

    /**
     * 新增数据
     *
     * @param dto 实例对象
     * @return 实例对象
     */
    @Override
    public Integer add(ArticleRecommendTypeDTO dto) {
        ArticleRecommendTypeDO entity = BeanUtil.toBean(dto, ArticleRecommendTypeDO.class);

        if (this.save(entity)) {
            return entity.getId();
        }

        return 0;
    }

    /**
     * 修改数据
     *
     * @param dto 实例对象
     * @return 实例对象
     */
    @Override
    public Boolean edit(ArticleRecommendTypeDTO dto) {
        return this.updateById(BeanUtil.toBean(dto, ArticleRecommendTypeDO.class));
    }


    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ArticleRecommendTypeVO getInfo(Integer id) {
        ArticleRecommendTypeDO entity = this.getById(id);

        return BeanUtil.toBean(entity, ArticleRecommendTypeVO.class);
    }

    /**
     * 分页查询
     *
     * @param dto 实例对象
     * @return 查询结果
     */
    @Override
    public Page<ArticleRecommendTypeVO> getPage(ArticleRecommendTypeDTO dto) {
        Page<ArticleRecommendTypeVO> page = new Page<>(dto.getPageIndex(), dto.getPageSize());
        return this.articleRecommendTypeMapper.getPage(page, dto);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public Boolean delete(Integer id) {
        return this.removeById(id);
    }

    /**
     * 获取最大排序
     *
     * @return 查询结果
     */
    @Override
    public Integer getMaxSort() {
        return this.articleRecommendTypeMapper.getMaxSort();
    }

    @Override
    public List<ArticleRecommendTypeVO> getList(int articleId) {
        List<ArticleRecommendTypeVO> list = new ArrayList<>();
        this.list().forEach(item -> {
            ArticleRecommendTypeVO vo = new ArticleRecommendTypeVO();
            vo.setId(item.getId());
            vo.setRecommendTypeName(item.getRecommendTypeName());
            LambdaQueryWrapper<ArticleRecommendDO> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ArticleRecommendDO::getArticleId, articleId);
            wrapper.eq(ArticleRecommendDO::getRecommendTypeId, item.getId());
            if (articleRecommendService.count(wrapper) > 0) {
                vo.setIsRecommend(true);
            } else {
                vo.setIsRecommend(false);
            }

            list.add(vo);
        });

        return list;
    }
}

